﻿@using StackExchange.Opserver.Controllers
@using StackExchange.Opserver.Data.Redis
@model RedisHost
@{
    var s = Model;
}
<h4 class="modal-title">
    Actions for @s.IconSpan() @s.HostName <span class="text-muted">(@s.ReplicationGroup)</span>
</h4>
@functions {
    public string CountSummary(List<RedisInstance> instances)
    {
        var sb = StringBuilderCache.Get();
        var masters = instances.Count(i => i.IsMaster);
        var slaves = instances.Count(i => i.IsSlave);
        if (masters > 0)
        {
            sb.Append(masters.Pluralize("master"));
        }
        if (slaves >0)
        {
            if (sb.Length > 0)
            {
                sb.Append(", ");
            }
            sb.Append(slaves.Pluralize("slave"));
        }
        return sb.ToStringRecycle();
    }
}
@helper ServerRole (RedisInstance instance) {
switch (instance.Role)
{
    case RedisInfo.RedisInstanceRole.Master:
        <span class="text-muted">(<span class="text-primary">master</span>)</span>
        break;
    case RedisInfo.RedisInstanceRole.Slave:
        if (instance.IsSlaving)
        {
            <span class="text-muted">(<b class="text-success">slaving</b> to <a href="#/redis/actions/@instance.Master.Host.HostName.UrlEncode()">@instance.Master.Host</a>)</span>
        }
        else
        {
            <span class="text-muted">(slave of <a href="#/redis/actions/@instance.Master.Host.HostName.UrlEncode()">@instance.Master.Host</a>)</span>
        }
        break;
    default:
        <span class="text-muted">(<span class="text-warning">unknown</span>)</span>
        break;
    }
}
@helper HostTree(RedisHost host, List<RedisInstance> filterTo = null)
{
    filterTo = filterTo ?? host.Instances.Where(i => i.IsMaster).ToList();
    var downstream = host.GetDownstreamHosts(filterTo);

    <li>
        <span title="@string.Join("\n", filterTo.Select(fi => fi.Name))">
            @host.IconSpan()
            @if (host == Model)
            {
                <strong>@host.HostName</strong>
            }
            else
            {
                <a href="#/redis/actions/@host.HostName.UrlEncode()">@host.HostName</a>
            }
            <span class="text-muted">(@CountSummary(filterTo ?? host.Instances.Where(i => i.IsMaster).ToList()))</span>
        </span>
        @if (downstream.Count > 0)
        {
            <ul class="ul-tree">
                @foreach (var dsHost in downstream)
                {
                    @HostTree(dsHost.TargetHost, dsHost.ReplicatedInstances)
                }
            </ul>
        }
    </li>
}
<div class="js-redis-actions" data-node="@s.HostName">
    <div class="row small">
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">Topology for @s.ReplicationGroup Group</div>
                <div class="panel-body">
                    <ul class="ul-tree">
                        @foreach (var h in s.ReplicationGroup.Hosts)
                        {
                            if (h.IsTopLevel)
                            {
                                @HostTree(h)
                            }
                        }
                    </ul>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">@s.HostName instances</div>
                <div class="panel-body">
                    <div class="row col-md-12">
                        @foreach (var i in s.Instances)
                        {
                            <div>
                                @i.IconSpan() <a href="~/redis/instance?node=@i.HostAndPort">@i.Port</a><span class="text-muted">:</span> @i.Name 
                                <div class="pull-right">
                                    @ServerRole(i)
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">Replication Operations</div>
                <div class="panel-body js-server-actions-selection" data-preview-url="@Url.Action(nameof(RedisController.ServerActionPreview), "Redis")">
                    <div class="list-group list-group-condensed">
                        @if (s.Instances.Count(i => i.IsSlave) > 0)
                        {
                            <a class="list-group-item js-redis-host-list">
                                <span class="text-primary"><i class="fa fa-arrow-up" aria-hidden="true"></i> Make Standalone</span>
                                <span class="pull-right"><i class="fa fa-chevron-right" aria-hidden="true"></i></span>
                            </a>
                            <div class="well well-sm well-condensed hidden">
                                <div class="row small">
                                    @foreach (var i in s.Instances)
                                    {
                                        var disabled = i.IsMaster;
                                        var defaultChecked = i.IsSlave;
                                        <div class="checkbox col-md-12">
                                            <label title="@(disabled ? "Already a master" : null)">
                                                <input type="checkbox" name="operations" value="@InstanceCommandType.MakeMaster|@i.UniqueKey" checked="@defaultChecked" disabled="@disabled"> @i.IconSpan() <span class="text-muted">@i.Port:</span> @i.Name
                                            </label>
                                        </div>
                                    }
                                </div>
                            </div>
                        }
                        @foreach (var host in s.ReplicationGroup.Hosts.Where(h => h != s))
                        {
                            var sameRegion = s.InSameRegionAs(host);
                            if (s.CanSlaveTo(host, out var reason))
                            {
                                <a class="list-group-item js-redis-host-list">
                                    Slave to @host.IconSpan() @host.HostName
                                    @if (!sameRegion)
                                    {
                                        <span class="text-muted">(cross-region)</span>
                                    }
                                    <span class="pull-right"><i class="fa fa-chevron-right" aria-hidden="true"></i></span>
                                </a>
                                <div class="well well-sm well-condensed hidden">
                                    <div class="row small">
                                        @foreach (var i in host.Instances)
                                        {
                                            var matching = s.Instances.FirstOrDefault(si => si.Port == i.Port && si.Name == i.Name);
                                            if (matching == null)
                                            {
                                                continue;
                                            }
                                            var disabled = i.SlaveInstances.Any(si => si.Host == Model);
                                            var defaultChecked = sameRegion || i.ReplicatesCrossRegion;
                                            <div class="checkbox col-md-12">
                                                <label title="@(disabled ? "Already replicating" : null)">
                                                    <input type="checkbox" name="operations" value="@InstanceCommandType.SlaveTo|@matching.UniqueKey|@i.UniqueKey" checked="@defaultChecked" disabled="@disabled"> @i.IconSpan() <span class="text-muted">@i.Port:</span> @i.Name
                                                </label>
                                            </div>
                                        }
                                    </div>
                                </div>
                            }
                            else
                            {
                                <a href="#" class="list-group-item disabled">
                                    <span class="text-muted">@host.IconSpan() @host.HostName</span>
                                    <div class="text-muted">@reason</div>
                                </a>
                            }
                        }
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">Preview</div>
                <div class="panel-body">
                    <div class="js-server-action-preview">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="pull-right">
            <button class="btn btn-sm btn-primary js-server-action-execute" type="button" data-perform-url="@Url.Action(nameof(RedisController.ServerActionPerform), "Redis")" disabled>Select operations above...</button>
        </div>
    </div>
</div>